
package cz.cvut.fel.y36mm1.videostreaming;

import cz.cvut.fel.y36mm1.videostreaming.devicediscovery.DeviceDiscoverer;
import cz.cvut.fel.y36mm1.videostreaming.devicediscovery.DevicesScreen;
import cz.cvut.fel.y36mm1.videostreaming.exceptions.ServerCannotBeClientException;
import cz.cvut.fel.y36mm1.videostreaming.screens.ErrorScreen;
import javax.bluetooth.BluetoothStateException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

/**
 * Tohle je jediny obsluhovac udalosti v cele aplikaci a veskere udalosti by
 * se mely zpracovavat tady.
 * @author Jiri Chadima, chadij1
 */
public class Commander implements CommandListener {

	public static Command START_SERVER = new Command("Start server", Command.SCREEN, 0);
	public static Command CONFIRM_DEVICE = new Command("Confirm device", Command.OK, 0);
	public static Command RESTART_INQUIRY = new Command("Restart inquiry", Command.ITEM, 0);
	public static Command STOP_SERVER = new Command("Stop server", Command.SCREEN, 0);
	public static Command STOP_CLIENT = new Command("Stop client", Command.SCREEN, 0);
	public static Command TEST_START_CLIENT = new Command("Start client", Command.SCREEN, 0);
	public static Command SEND_SNAPSHOT = new Command("Send snapshot", Command.ITEM, 0);
	public static Command ERROR_BACK = new Command("Back", Command.SCREEN, 0);


	public static Command EXIT = new Command("Exit", Command.EXIT, 0);

	public MainMidlet root;
	
	public Commander(MainMidlet root) {
		this.root = root;
	}


	public void commandAction(Command c, Displayable d) {
		if(c.equals(START_SERVER)) { //start server
			root.initiateServer();
		} if(c.equals(TEST_START_CLIENT)) { // only for debug reasons
			root.initiateClient("");
		} else if(c.equals(STOP_SERVER)) { //stops server
			root.terminateServer();

		} else if(c.equals(STOP_CLIENT)) { //stops client
			root.terminateClient();

		} else if(c.equals(SEND_SNAPSHOT)) { //sends snapshot
			try {
				root.getDevice().sendSnapshot();
			} catch (ServerCannotBeClientException ex) {
				// this should never happen
				ErrorHandler.get().displayError("Error no. 4353, call tech support. Your phone is going to blow up. This is joke.", d);
			}

		}  else if(c.equals(RESTART_INQUIRY)) { // start remote devices inquiry again
			try {
				if(d instanceof DevicesScreen) {
					DeviceDiscoverer.getInstance().getDevicesScreen().getChoices().deleteAll();
					//DeviceDiscoverer.getInstance().getDevicesScreen().delete(1); // search completed hlasku vymazu // currently not used
					DeviceDiscoverer.getInstance().startInquiry();
				}
			} catch (BluetoothStateException ex) {
				//ex.printStackTrace();
			}
		} else if(c.equals(CONFIRM_DEVICE)) { // in fact, starts client and connects it to selected device

			//try {
				int selected = 0;
				selected = DeviceDiscoverer.getInstance().getDevicesScreen().getChoices().getSelectedIndex(); // get selected choice
				String deviceName = DeviceDiscoverer.getInstance().getDevicesScreen().getChoices().getString(selected); // get text representation of that device
				String serviceURL = (String) DeviceDiscoverer.getInstance().getDevices().get(deviceName); // get devices url
				root.initiateClient(serviceURL); // start client
			//} catch (NullPointerException e) { // this didn't work properly, so was taken out
			//	ErrorHandler.get().displayError("Cannot connect to server. Restart application and try again, please.", d);
			//}
			
		} else if(c.equals(EXIT)) {
			root.destroyApp(true);
			root.notifyDestroyed();
		} else if(c.equals(ERROR_BACK)) {
			if(d instanceof ErrorScreen) {
				ErrorScreen es = (ErrorScreen) d;
				
				root.getDisplay().setCurrent(es.getHistoryScreen());
			}

		}
	}

}
